<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Menu extends Widget
{

    public function render($options)
    {
//        $ci =& get_instance();
        $this->ci->load->model('navigation/navigation_model');

        $lang = $this->ci->config->item('language_abbr');

        $menu_items = $this->ci->navigation_model->get_menu_by_name($options['menu_name'], $lang);

        if (count($menu_items)>0)
        {
            $items = array();
            foreach ($menu_items as $menu_item)
            {
                $cur_uri = trailingslashit( uri_string() );
                $cur_uri = str_replace(leadingslashit($lang), '', $cur_uri);
                $cur_class = '';
                if ($menu_item->uri == '') {
                    $the_uri =  trailingslashit( $menu_item->uri );
                    if ($cur_uri==$the_uri) {
                        $cur_class = 'class="current"';
                    }
                }
                else {
                    $the_uri = trailingslashit( leadingslashit($menu_item->uri) );
                    if (strpos($cur_uri, $the_uri)===0) {
                        $cur_class = 'class="current"';
                    }
                }

    //            echo 'the uri = ' . $the_uri . '<br/>';
    //            echo 'cur uri = ' . $cur_uri . '<br/>';
    //            echo '<hr/>';

                $items[] = anchor($menu_item->uri, $menu_item->title, $cur_class);
            }
            echo ul( $items, array('class'=> 'menu-' . $options['menu_name']) );
        }
    }

}